package com.kj.xuyuan.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.UniversalPopAdapter;
import com.kj.xuyuan.util.ScreenUtils;

import java.util.List;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建数据填充的popupwindow，多个纵向排列，ui相同的弹窗使用
 * 这个项目主要用在各个页面右上角的更多选项中
 */
public class UniversalClickPopWindow extends PopupWindow {

    @BindView(R.id.rv_click)
    RecyclerView rvClick;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private Activity mContext;
    private View view;
    private List<String> data;
    private ItemClickListener listener;

    public UniversalClickPopWindow(Activity context, List<String> data) {
        super(context);
        this.mContext = context;
        this.data = data;
        // 初始化页面
        init();
    }

    // 初始化布局
    private void init() {
        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_pop_universal_click, null);
        ButterKnife.bind(this, view);
        setFocusable(true);

        //rvClick 传入需要点击的数据 如举报和拉黑 item item_universal
        UniversalPopAdapter adapter = new UniversalPopAdapter();
        rvClick.setLayoutManager(new LinearLayoutManager(mContext));
        rvClick.setAdapter(adapter);
        adapter.setNewData(data);

        adapter.setCount(data.size());

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if(listener != null){
                    listener.click(position);
                }
                dismiss();
            }
        });

        // 导入布局
        this.setContentView(view);
        this.setClippingEnabled(false);
        setWidth(ScreenUtils.getScreenWidth(mContext));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        final ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);
        setAlpha(0.7f);
    }

    @Override
    public void dismiss() {
        setAlpha(1);
        super.dismiss();
    }

    private void setAlpha(float alpha) {
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = alpha;
        mContext.getWindow().setAttributes(params);
    }

    @OnClick(R.id.tv_cancel)
    public void onClick() {
        dismiss();
    }

    public void setItemClickListener(ItemClickListener listener){
        this.listener = listener;
    }

    public interface ItemClickListener{
        void click(int position);
    }
}
